package xnj.lazydog.btcontroller.ControlViews.ControlViews;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import xnj.lazydog.btcontroller.ControlViews.Value;
import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.R;

public class ControlValueDragBar extends ControlElement {

    public ControlValueDragBar (ViewOptions options)
    {
        super(options);


    }


    float TRACK_WIDTH_RATIO = 0.48f;
    float THUMB_RADIUS_RATIO = 0.45f;

    float thumb_pix;

    float progress,progress_d,progress_dt,thumb_x_pix;

    float bar_length,border_length;
    @Override
    public void onSizeChanged() {

        float h_pix = this.h_pix;

        if(h_pix>w_pix*0.15f)
            h_pix = w_pix*0.15f;

        thumb_pix = h_pix*THUMB_RADIUS_RATIO;

        edge_pix = (h_pix)*EDGE_SIZE_RATIO*1.5f;

        if(edge_pix>max_edge_pix)
            edge_pix = max_edge_pix;

        if(edge_pix<min_edge_pix)
            edge_pix = min_edge_pix;

        p.setStrokeWidth(edge_pix);

        border_length = w_pix-(thumb_pix-TRACK_WIDTH_RATIO*0.5f*h_pix)*2;
        bar_length = w_pix-thumb_pix*2;

        rectF.right = border_length*0.5f;
        rectF.left = -rectF.right;
        rectF.bottom = h_pix*TRACK_WIDTH_RATIO*0.5f;
        rectF.top = -rectF.bottom;


    }

    @Override
    public void draw(Canvas canvas) {

        if(linkedValues[0]==null) {
            progress = 0.5f;
        }else
            progress = (linkedValues[0].getFloat()-options.off_value)/(options.on_value-options.off_value);

        if(progress>1)
            progress = 1;
        if(progress<0)
            progress = 0;

        progress_dt = progress-progress_d;

        if(Math.abs(progress_dt)>0.005)
        {
            progress_d+=progress_dt*0.35f;
        }else
        {
            progress_d = progress;
        }


        thumb_x_pix  = bar_length*(-0.5f+progress_d);


        p.setStyle(Paint.Style.FILL);
        p.setColor(THEME_GRAY);
        canvas.drawRoundRect(rectF,rectF.bottom,rectF.bottom,p);

        p.setColor(THEME_WHITE);
        canvas.drawCircle(thumb_x_pix,0,thumb_pix,p);

        p.setStyle(Paint.Style.STROKE);

        p.setColor(THEME_EDGE);
        canvas.drawCircle(thumb_x_pix,0,thumb_pix,p);


    }

    @Override
    public void clicked(Context context) {

        showSettingsDialog(context);

    }

    private static final String TAG = "ControlValueDragBar";


    @Override
    public void dragged() {

        if(this.rx>0) {
            float rx = 0.5f+(this.rx-0.5f)*1.1f;

            if(rx<0)
                rx = 0;
            if(rx>1)
                rx=1;

            if (linkedValues[0] != null) {
                if (linkedValues[0].type == 4)
                {

                    linkedValues[0].set(options.off_value + (options.on_value - options.off_value) * rx);

                } else {

                    int delta = (int) (options.on_value - options.off_value);
                    if (delta == 0)
                        linkedValues[0].set(options.off_value);
                    else {
                        float div_half = 0.5f / delta;
                        int i = (int) (options.off_value + (rx + div_half) * delta);
                        linkedValues[0].set(i);
                    }
                }
                triggerDataEvent();
            }
        }
    }
    @Override
    public void released() {
    }
    @Override
    public void pressed() {
    }
    @Override
    public void zoomed(float sx, float sy) {
    }




    @Override
    public void showSettingsDialog(Context context) {

        if(linkedValues[0]==null) {
            Toast.makeText(context,"请先链接一个变量",Toast.LENGTH_SHORT).show();
            return;
        }
        final EditText on_edit,off_edit;
        Button ok;
        TextView info;
        String str;
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View layout = LayoutInflater.from(context).inflate(R.layout.toy_settings_value_bar,null);
        builder.setView(layout);
        dialog = builder.create();
        on_edit = layout.findViewById(R.id.on_edit);
        off_edit = layout.findViewById(R.id.off_edit);
        info = layout.findViewById(R.id.button_info);
        ok = layout.findViewById(R.id.button);

        if(options.on_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.on_value));
            else
                str=(""+options.on_value);
        }else
            str="";

        on_edit.setText(str);
        on_edit.setSelection(str.length());

        if(options.off_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.off_value));
            else
                str=(""+options.off_value);
        }else
            str ="";

        off_edit.setText(str);
        off_edit.setSelection(str.length());

        if(linkedValues[0].type<4)
            str = "此控件链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(int)(MIN_VALUES[linkedValues[0].type])+"~"+(int)(MAX_VALUES[linkedValues[0].type]);
        else
            str = "此控件链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(MIN_VALUES[linkedValues[0].type])+"~"+(MAX_VALUES[linkedValues[0].type]);

        info.setText(str);

        initLimitedEdit(on_edit,info,linkedValues[0].type);
        initLimitedEdit(off_edit,info,linkedValues[0].type);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Value value = getNumber(on_edit.getText().toString());
                if(value.type==0)
                {
                    options.on_value = 0;
                    options.on_en = true;
                }else
                {
                    options.on_value = value.getFloat();
                    options.on_en = true;
                }

                value = getNumber(off_edit.getText().toString());
                if(value.type==0)
                {
                    options.off_value = 0;
                    options.off_en = true;
                }else
                {
                    options.off_value = value.getFloat();
                    options.off_en = true;
                }

                options.threshold = (options.on_value+options.off_value)*0.5f;
                dialog.dismiss();
            }
        });
        dialog.show();
    }



}
